package com.i2works.smartluolong.web.ocr;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;
import com.i2works.smartluolong.utils.common.HttpUtils;
import com.i2works.smartluolong.utils.common.ImageConverUtil;
/**
 * 表格文字识别
 * */
public class ocrTest2 {
	
	public static void main(String[] args) {
	    String host = "https://form.market.alicloudapi.com";
	   String path ="/api/predict/ocr_table_parse";
	    String method = "POST";
	    String appcode = "0b987ff4178648c3be2c16f4d6a9b66b";
	    Map<String, String> headers = new HashMap<String, String>();
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + appcode);
	    //根据API的要求，定义相对应的Content-Type
	    headers.put("Content-Type", "application/json; charset=UTF-8");
	    Map<String, String> querys = new HashMap<String, String>();
	 //   String bodys = "{\"image\":\"图片文件内容的base64编码\",\"configure\":\"{\"min_size\":16,#图片中文字的最小高度\"output_prob\":true#是否输出文字框的概率}\"}";

	    JSONObject  jsonObject=new 	 JSONObject();
	    String imageBase64 = ImageConverUtil.toBase64("D:\\201883016048498.jpg");
	    jsonObject.put("image",imageBase64);
       JSONObject  jsonConfigure=new 	 JSONObject();
	    jsonConfigure.put("format","json");
	    jsonConfigure.put("finance", false);
	    jsonConfigure.put("dir_assure", false);
	    jsonObject.put("configure", jsonConfigure);
	    String bodys=jsonObject.toJSONString();
	    System.out.println(bodys);
	    try {
	    HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
	    	System.out.println(response.toString());
	    	//获取response的body
	    	System.out.println(EntityUtils.toString(response.getEntity()));
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	}

}
